/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.EJB.sessionBeans;

import it.polimi.meteocal.EJB.entities.Event;
import it.polimi.meteocal.EJB.entities.Invite;
import it.polimi.meteocal.EJB.entities.InviteNotification;
import it.polimi.meteocal.EJB.entities.ModificationNotification;
import it.polimi.meteocal.EJB.entities.User;
import it.polimi.meteocal.EJB.entities.WeatherNotification;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.ejb.EJBContext;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Fabio
 */
@Stateless
public class NotificationManager {

    @PersistenceContext
    private EntityManager em;

    public List<InviteNotification> InviteNotificationList(String username) {
        Query q = em.createNativeQuery("SELECT id FROM invite_notification WHERE (receiver_user = '" + username + "') ORDER BY rec_date DESC");
        List<Long> iNotifList = q.getResultList();
        List<InviteNotification> notifList = new ArrayList<InviteNotification>();
        for (Long id : iNotifList) {
            Integer ad = id.intValue();
            int ID = ad.intValue();
            InviteNotification n = em.find(InviteNotification.class, ID);

            Date d = new Date();

            if (d.getYear() < n.getInvite().getEvent().getDate().getYear()) {
                notifList.add(n);
            }

            if (d.getYear() == n.getInvite().getEvent().getDate().getYear()) {
                if (d.getMonth() < n.getInvite().getEvent().getDate().getMonth()) {
                    notifList.add(n);
                }
            }

            if (d.getYear() == n.getInvite().getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getInvite().getEvent().getDate().getMonth()) {
                    if (d.getDate() < n.getInvite().getEvent().getDate().getDate()) {
                        notifList.add(n);
                    }
                }
            }

            if (d.getYear() == n.getInvite().getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getInvite().getEvent().getDate().getMonth()) {
                    if (d.getDate() == n.getInvite().getEvent().getDate().getDate()) {
                        if (d.getHours() < n.getInvite().getEvent().getBeginningHour().getHours()) {
                            notifList.add(n);
                        }
                    }
                }
            }

            if (d.getYear() == n.getInvite().getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getInvite().getEvent().getDate().getMonth()) {
                    if (d.getDate() == n.getInvite().getEvent().getDate().getDate()) {
                        if (d.getHours() == n.getInvite().getEvent().getBeginningHour().getHours()) {
                            if (d.getMinutes() < n.getInvite().getEvent().getBeginningHour().getMinutes()) {
                                notifList.add(n);
                            }
                        }
                    }
                }
            }
        }

        return notifList;

    }

    public List<WeatherNotification> WeatherNotificationList(String username) {
        Query q = em.createNativeQuery("SELECT id FROM weather_notification WHERE receiver_user = '" + username + "' ORDER BY rec_date DESC");
        List<Long> wNotifList = q.getResultList();
        List<WeatherNotification> notifList = new ArrayList<WeatherNotification>();
        for (Long id : wNotifList) {
            Integer ad = id.intValue();
            int ID = ad.intValue();
            WeatherNotification n = em.find(WeatherNotification.class, ID);

            Date d = new Date();

            if (d.getYear() < n.getEvent().getDate().getYear()) {
                notifList.add(n);
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() < n.getEvent().getDate().getMonth()) {
                    notifList.add(n);
                }
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getEvent().getDate().getMonth()) {
                    if (d.getDate() < n.getEvent().getDate().getDate()) {
                        notifList.add(n);
                    }
                }
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getEvent().getDate().getMonth()) {
                    if (d.getDate() == n.getEvent().getDate().getDate()) {
                        if (d.getHours() < n.getEvent().getBeginningHour().getHours()) {
                            notifList.add(n);
                        }
                    }
                }
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getEvent().getDate().getMonth()) {
                    if (d.getDate() == n.getEvent().getDate().getDate()) {
                        if (d.getHours() == n.getEvent().getBeginningHour().getHours()) {
                            if (d.getMinutes() < n.getEvent().getBeginningHour().getMinutes()) {
                                notifList.add(n);
                            }
                        }
                    }
                }
            }
        }

        return notifList;

    }

    public List<ModificationNotification> ModificationNotificationList(String username) {
        User u = em.find(User.class, username);
        List<Long> mNotifList = new ArrayList<Long>();
        int i = 0;
        while (i < u.getInviteList().size()) {
            Query q = em.createNativeQuery("SELECT id FROM modification_notification WHERE receiver_user = '" + username + "' ORDER BY rec_date DESC");
            List<Long> idList = q.getResultList();
            if (idList.size() > 0) {
                mNotifList.add(idList.get(0));
            }
            i++;
        }
        List<ModificationNotification> notifList = new ArrayList<ModificationNotification>();
        for (Long id : mNotifList) {
            Integer ad = id.intValue();
            int ID = ad.intValue();
            ModificationNotification n = em.find(ModificationNotification.class, ID);
            Date d = new Date();

            if (d.getYear() < n.getEvent().getDate().getYear()) {
                notifList.add(n);
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() < n.getEvent().getDate().getMonth()) {
                    notifList.add(n);
                }
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getEvent().getDate().getMonth()) {
                    if (d.getDate() < n.getEvent().getDate().getDate()) {
                        notifList.add(n);
                    }
                }
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getEvent().getDate().getMonth()) {
                    if (d.getDate() == n.getEvent().getDate().getDate()) {
                        if (d.getHours() < n.getEvent().getBeginningHour().getHours()) {
                            notifList.add(n);
                        }
                    }
                }
            }

            if (d.getYear() == n.getEvent().getDate().getYear()) {
                if (d.getMonth() == n.getEvent().getDate().getMonth()) {
                    if (d.getDate() == n.getEvent().getDate().getDate()) {
                        if (d.getHours() == n.getEvent().getBeginningHour().getHours()) {
                            if (d.getMinutes() < n.getEvent().getBeginningHour().getMinutes()) {
                                notifList.add(n);
                            }
                        }
                    }
                }
            }

        }

        for(int h=0; h<notifList.size()-1; h++){
            for(int j=1; j<notifList.size(); j++){
                if(notifList.get(h).getRecDate().before(notifList.get(j).getRecDate()) ||
                (!notifList.get(h).getRecDate().before(notifList.get(j).getRecDate()) && !notifList.get(h).getRecDate().after(notifList.get(j).getRecDate()))){
                    ModificationNotification n= notifList.get(h);
                    notifList.set(h, notifList.get(j));
                    notifList.set(j, n);
                }
                else if(notifList.get(h).getRecDate().after(notifList.get(j).getRecDate())){
                    notifList.set(h, notifList.get(h));
                    notifList.set(j, notifList.get(j));
                }
            }
        }
        
        return notifList;

    }

    public User findUser(String username) {

        return em.find(User.class, username);
    }

    public void createWeatherNotification(User user, Event event) {
        WeatherNotification newNotif = new WeatherNotification();
                    Date d= new Date();
        Query q = em.createNativeQuery("SELECT id FROM weather_notification WHERE (receiver_user = '" + user.getUsername() + "' AND event ='" + event.getId() + "')");
        if (q.getResultList().isEmpty()) {
            newNotif.setEvent(event);
            newNotif.setReceiverUser(user);
            newNotif.setId(0);
            newNotif.setRecDate(d);
            em.persist(newNotif);
        }
        
        else{
        List<Long> wNotifList = q.getResultList();
        List<WeatherNotification> wNotif = new ArrayList<WeatherNotification>();
        
        for (Long id : wNotifList) {
            Integer ad = id.intValue();
            int ID = ad.intValue();
            WeatherNotification n = em.find(WeatherNotification.class, ID);
            wNotif.add(n);
        }
        
        List<WeatherNotification> todayNotif = new ArrayList<WeatherNotification>();

        for(int i=0; i<wNotif.size(); i++){
            if(wNotif.get(i).getRecDate().getDate()==d.getDate() && wNotif.get(i).getRecDate().getMonth()==d.getMonth() &&
                    wNotif.get(i).getRecDate().getYear()==d.getYear()){
                todayNotif.add(wNotif.get(i));
            }
        }
        
        if(todayNotif.isEmpty()){
            newNotif.setEvent(event);
            newNotif.setReceiverUser(user);
            newNotif.setId(0);
            newNotif.setRecDate(d);
            em.persist(newNotif);
        }
        }
        
    }
    
       public List<Event> createdEvents(String a) {
        Query q
                = em.createNativeQuery("SELECT id FROM Event WHERE organizer='" + a + "' ORDER BY date,beginning_hour ASC");
        List<Long> allEv = q.getResultList();
        List<Event> allEvents = new ArrayList<Event>();
        for (Long id : allEv) {
            Integer ad = id.intValue();
            int ID = ad.intValue();
            Event e = em.find(Event.class, ID);
            Date d = new Date();

            if (d.getYear() < e.getDate().getYear()) {
                allEvents.add(e);
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() < e.getDate().getMonth()) {
                    allEvents.add(e);
                }
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() == e.getDate().getMonth()) {
                    if (d.getDate() < e.getDate().getDate()) {
                        allEvents.add(e);
                    }
                }
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() == e.getDate().getMonth()) {
                    if (d.getDate() == e.getDate().getDate()) {
                        if (d.getHours() < e.getBeginningHour().getHours()) {
                            allEvents.add(e);
                        }
                    }
                }
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() == e.getDate().getMonth()) {
                    if (d.getDate() == e.getDate().getDate()) {
                        if (d.getHours() == e.getBeginningHour().getHours()) {
                            if (d.getMinutes() < e.getBeginningHour().getMinutes()) {
                                allEvents.add(e);
                            }
                        }
                    }
                }
            }
        }
        return allEvents;
    }
    
    
    public List<Event> invitedEvents(String a) {
        Query q
                = em.createNativeQuery("SELECT id FROM Event WHERE ( id IN (SELECT event FROM Invite WHERE guest='" + a + "') ) ORDER BY date,beginning_hour ASC");
        List<Long> allEv = q.getResultList();
        List<Event> allEvents = new ArrayList<Event>();
        for (Long id : allEv) {
            Integer ad = id.intValue();
            int ID = ad.intValue();
            Event e = em.find(Event.class, ID);
            Date d = new Date();

            if (d.getYear() < e.getDate().getYear()) {
                allEvents.add(e);
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() < e.getDate().getMonth()) {
                    allEvents.add(e);
                }
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() == e.getDate().getMonth()) {
                    if (d.getDate() < e.getDate().getDate()) {
                        allEvents.add(e);
                    }
                }
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() == e.getDate().getMonth()) {
                    if (d.getDate() == e.getDate().getDate()) {
                        if (d.getHours() < e.getBeginningHour().getHours()) {
                            allEvents.add(e);
                        }
                    }
                }
            }

            if (d.getYear() == e.getDate().getYear()) {
                if (d.getMonth() == e.getDate().getMonth()) {
                    if (d.getDate() == e.getDate().getDate()) {
                        if (d.getHours() == e.getBeginningHour().getHours()) {
                            if (d.getMinutes() < e.getBeginningHour().getMinutes()) {
                                allEvents.add(e);
                            }
                        }
                    }
                }
            }
        }
        return allEvents;
    }
    
    
}
